package com.xxcloud.search.service.note.impl;

import java.util.List;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.xxcloud.model.web.ListResult;
import com.xxcloud.search.api.bean.note.NoteApiParameter;
import com.xxcloud.search.api.bean.note.NoteItem;
import com.xxcloud.search.model.note.NoteModel;
import com.xxcloud.search.service.note.INoteSearchService;

/**
 * Created by chenbingwen on 19/10/31.
 */
@Service("noteSearchService")
public class NoteSearchServiceImpl extends AbstractNoteSearchService implements INoteSearchService {

    private final Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;

    @Override
    public ListResult<NoteItem> search(NoteApiParameter parameter) {
        logger.debug("noteSearchService search parameter:{}", JSON.toJSONString(parameter));
        ListResult<NoteItem> result = new ListResult<>();
        // q
        BoolQueryBuilder boolQueryBuilder = buildBoolQuery(parameter);
        // fq
        BoolQueryBuilder boolFilterQueryBuilder = buildFilterQuery(parameter);
        // 设置排序
        FieldSortBuilder sortBuilder = buildFieldSortBuilder(parameter);
        // 设置分页
        PageRequest pageRequest = buildPageRequest(parameter);

        // 创建查询生成器
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        nativeSearchQueryBuilder.withQuery(boolQueryBuilder);
        nativeSearchQueryBuilder.withFilter(boolFilterQueryBuilder);
        nativeSearchQueryBuilder.withSort(sortBuilder);
        nativeSearchQueryBuilder.withPageable(pageRequest);

        NativeSearchQuery nativeSearchQuery = nativeSearchQueryBuilder.build();

        logger.debug("q:{}", buildDSLQuery(nativeSearchQuery.getQuery()));
        logger.debug("fq:{}", buildDSLQuery(nativeSearchQuery.getFilter()));

        Page<NoteModel> pageNoteModels = elasticsearchTemplate.queryForPage(nativeSearchQuery, NoteModel.class);

        long total = pageNoteModels.getTotalElements();
        if (total > 0) {
            List<NoteItem> noteItems = convert2NoteItems(pageNoteModels.getContent());
            result.setResult(noteItems);
        }
        result.setTotal((int) total);
        logger.debug("noteSearchService search result:{}", JSON.toJSONString(result));
        return result;
    }

    @Override
    public void aggregation(NoteApiParameter parameter) {

    }

}
